        TITL DAI FIRMWARE D620-D78F  V1.1
        ORG   :D620
*
*
*
**************************
* COMPLETE KEYBOARD SCAN *
**************************
*
* Initialises a complete keyboard scan,
* independent of the KNSCAN flag, and performs it.
*
* Exit: All registers preserved.
*
KFSCAN  PUSH  PSW
        PUSH  B
        PUSH  D
        PUSH  H
        LXI   H,KNSCAN  Addr KNSCAN pointer
        MVI   M,:00     Enable complete scan
        PUSH  H
        CALL  KBSCAN    Scan keyboard and store
 REM result in circ.buffer
        POP   H
        DCR   M         Scan for BREAK only
        JMP   LC188     Popall; return
*
**********************************
* GET ASCII VALUE OF KEY PRESSED *
**********************************
*
* Calculates address in ASCII table in ROM and gets
* ASCII value of the key pressed. The result is
* stored in the 4-bye circular buffer KLIND.
*
* Entry: A:  Keycode of scanned row (7 bits only).
*        B:  FF when REPT pressed; else 00.
*        C:  Number of row.
*        DE: Address in MAP2.
*        HL: Address in MAP1.
* Exit:  BCDEHL preserved; AF corrupted.
*
TKEY    PUSH  B
        MVI   B,:07     Check which key in row is
TKY10   RAR             pressed; calculate offset
        CC    SINKEY    Get ASCII value; store it
 REM in KLIND
        DCR   B
        JNZ   TKY10     Next column
        POP   B
        RET 
*
* GET KEY-ASCII VALUE AND STORE IT:
*
SINKEY  RST   1         Get ASCII-value from ROM-
        DATA  :12       table and store it in KLIND
        RET 
*
*******************************
* OUTPUT TO RS232 IF REQUIRED *
*******************************
*
* Checks if output is to RS232. If positive,
* output is performed.
*
* Entry: Byte to be transmitted in A.
*
TOUTSE  PUSH  PSW
        LDA   OTSW      Get output direction
        ORA   A         Check if RS232 output
        JNZ   OTC99     Abort if not
        POP   PSW
        JMP   OUTSE     Output to RS232
*
******************************************
* GET ASCII-VALUE OF CHARACTER IN BUFFER *
******************************************
*
* Routine is not used.
*
* The Ascii-value of a character is stored in
* KLIND. Afterwards, Bank select is restored.
*
LD67    CALL  INKEY     (3) Ascii-value in KLIND
        POP   PSW       A contains POROM
        CALL  MPT53     Update PORO and POROM
        POP   PSW
        POP   H
        RET 
*
************************************
* parts of RUN 'RANDOMISE' (0E40C) *
************************************
*
*RMI15
MPT39   DCR   A
        JNZ   MPT39     Again till A=0
        MOV   A,M       Get contents FD00
        XRA   E
        RET 
*
* Entry: L = 0.
*
MPT38   LDA   KBXCT     Get keyb.scan time count
 REM (0, 1 or 2)
        RRC 
        RRC             A= 0, #40 or #80
        MOV   E,A       in E
        MOV   B,L       )
        MOV   C,L       ) BC=0
        RET 
*
************************************
* WRITE 2 BLOCKS + TRAILER ON TAPE *
************************************
*
* Entry: HL:    Startaddress 1st block.
*        Stack: Length 2nd block.
*               Startaddress 2nd block.
*
MPT13   LXI   D,:0001   Length 1st block = 1
        CALL  WBLK      Write 1st block
        POP   D         Get length 2nd block
        POP   H         Get startaddr. 2nd block
        CALL  WBLK      Write 2nd block
        CALL  WCLOSE    Write trailer
        ORA   A
        RET 
*
********************************
* LOADA: EVALUATE PROGRAM NAME *
********************************
*
* The program name is evaluated. Selection of
* ROM bank 1 is prepared.
*
MPT14   CALL  MPT15     Evaluate program name
        LDA   POROM     Get POROM
        ORI   :40       Prepare selection ROMbank 1
        RET 
*
******************
* OPEN READ FILE *
******************
*
MPT18   PUSH  D
        CALL  ROPEN     Open READ file
        POP   D
        RET 
*
*********************************
* EVALUATE A STRING EXPRESSSION *
*********************************
*
* A string expression is evaluated. Eventually,
* the Heap entry is cleared if the string was
* temporarily on Heap.
*
* Exit: DE preserved, BC updated.
*       HL points after string
*
MPT15   PUSH  D
        CALL  REXSR     (0) Eval. string expr.
 REM evt. clear Heap entry
        POP   D
        RET 
*
*******************
* CURSOR HANDLING *
*******************
*
* Load the cursor pointers with the address, the
* colour and the contents of a new cursor address.
*
* Entry: HL: New cursor address.
*        D:  The colour byte of this location.
*        E:  The contents of this location.
* Exit:  HL and DE exchanged; ABCF preserved.
*
SPT00   SHLD  CURSOR    Store cursor address
        XCHG
        SHLD  CURSV     Store cursor addr.contents
        RET 
*
************************
* OUTPUT ONE CHARACTER *
************************
*
* Output direction depending on OTSW.
* This routine is useable for all data output
* functions in machine language programs.
*
* Entry: Character for output in A.
* Exit:  All registers preserved.
*
MPT31   PUSH  PSW
        CALL  OUTC      Output character in A.
        POP   PSW
        RET 
*
        DATA  :FF
*
************************************************
* KEYB. SCANNING: UPDATE POINTER OUTPUT BUFFER *
************************************************
*
* Updates the pointer to the circular output
* buffer #02BA-#02BD.
*
* Entry: HL: KLIOU.
* Exit:  HL: Updated KLIOU.
*        AF corrupted. BCDE preserved.
*
KPTRU   INX   H         Incr. KLIOU
        MOV   A,L       Lobyte into A
        CPI   :BE       Buffer full?
        RNZ             Quit if not
        LXI   H,KLIND   Else: wrap around
        RET 
*
******************************************
* KEYBOARD SCANNING: CHECK IF NEW INPUTS *
******************************************
*
* Returns a flag if BREAK has been pressed or if
* there is a character available.
*
* Entry: No conditions.
* Exit:  BCDEHL preserved.
*        A: Difference KLIIN and KLIOU. 
*        CY=1: Break pressed.
*        CY=0, Z=1: No inputs.
*        CY=0, Z=0: New input available.
*
*ASKKEY
BREAK   PUSH  H
 REM
* If suspended:
 REM
        LXI   H,KBRFL   Addr break pntr
        MOV   A,M
        DCR   A
        CPI   :FE       Test if not 0 or FF
        JC    OTK10     Abort if break: CY=1
*
        LHLD  KLIOU     Get KLIOU
        LDA   KLIIN     Get KLIIN
        SUB   L         New keys pressed?
        STC 
        CMC             CY=0
OTK10   POP   H
        RET 
*
******************
* INPUT SCANNING *
******************
*
* Gets input from keyboard or DINC, depending on
* INSW (0296).
*
* FGETC: Gets a character, even if keyboard
*        scanning is turned off.
*  GETC: Returns a flag if break, and sets break
*        accepted. Returns also a flag if a
*        character is available.
*
* Exit: CY=1: Break pressed.
*       Z=1:  No inputs. Then A=0.
*       Else: Character in A.
*
FGETC   CALL  KFSCAN    Complete keyboard scan
GETC    JMP   MPT29     Check input keyb/DINC;
 REM scan for new keys pressed
MPR29   JC    GTC10     Jump if Break pressed.
        RZ              No new input or buffer full
 REM
* If inputs:
 REM
        PUSH  H
        LHLD  KLIOU     Get addr pntr output buffer
        MOV   A,M       Get ASCII char in A
        PUSH  PSW
        CALL  KPTRU     Update pntr
        POP   PSW
        SHLD  KLIOU     Re-instate KLIOU
        POP   H
        RET             CY=0
 REM
* If Break pressed:
 REM
GTC10   MVI   A,:FF     Flag 'break accepted'
        STA   KBRFL     Scan for break only
        RET             CY=1
*
*********************
* WAIT FOR SPACEBAR *
*********************
*
* Wait until spacebar (or break) is pressed.
*
* Entry: None.
* Exit:  CY=1: Break pressed.
*        CY=0: Space in A.
*        BCDEHL preserved.
*
WSPACE  CALL  FGETC     Input scanning
        RC              Abort if BREAK pressed
        CPI   :20       Check if spacebar
        JNZ   WSPACE    Wait for space bar
        ORA   A
        RET 
*
*************************
* part of LOADA (1EE0F) *
*************************
*
MPT20   XTHL            Orig. DE in HL, free RAM
 REM pntr on stack
        CALL  COMP      Compare DE-HL
        JNC   RLA15     If DE<=HL
        XCHG
RLA15   MOV   B,D       ) Lowest value in BC
        MOV   C,E       )
        POP   D
        POP   H
        XTHL
        JMP   L1E248    (1) Continu
*
*****************************************
* CHECK SUFFICIENT SCREEN RAM AVAILABLE *
* PREPARE SELECTION SPLIT MODE          *
*****************************************
*
SPT01   STC             CY=1
        LHLD  GTE       Get end area splitting mode
        CALL  SMKRM     (2) Ask for temporary area
        LXI   H,TABMA   (2) Startaddr table screen
 REM parameters split modes
        RET 
*
******************************************
* CHANGE FROM SPLIT TO FULL GRAPHIC MODE *
******************************************
*
SSM20   CALL  SPT01     Check suff. screen RAM
        JMP   SSM21     (2) Change split to full
*
*****************************************
* CHECK SUFFICIENT SCREEN RAM AVAILABLE *
* PREPARE FULL GRAPHICS MODE            *
*****************************************
*
SPTA2   CALL  SPT01     Check suff. screen RAM
        LXI   H,TABM    (2) Startaddr table screen
 REM parameters full graph.modes
        RET 
*
******************************
* SET UP CURRENT SCREEN MODE *
******************************
*
SMA20   POP   D
        POP   PSW
SMA30   LDA   COL1      Get current screen mode
        ORA   A
        RAR             Split or all-graph mode ?
        CALL  TABP      (2) Set up screen mode
        JMP   SS020     (2) Pop PSW, ret
*
*************************
* STOP LOADING PROGRAMS *
*************************
*
* Entry: HL: New end symbol table.
* Exit:  All registers preserved.
*
MPT11   SHLD  STBUSE    Store end symtab
        JMP   RCLOSE    Stop loading
*
*****************************
* INIT. WRITING FILE LEADER *
*****************************
*
* Procedure depends on saving in program or not.
*
MPT21   PUSH  H
        LHLD  CURRNT    Get start current line
        MOV   A,H
        ORA   L         0 if not during run
        POP   H
        JNZ   WHD20     If SAVE during run
        JMP   WHDR      Write file leader
*
******************************************
* INIT. SOUNDGENERATOR, GIC, START HEAP, *
* MOVE CASSETTE VECTORS, GET DCE INPUTS  *
******************************************
*
MPT00   CALL  SNDINI    Init. sound generator
        CALL  CASIN     Transfer cassette vectors
        LXI   H,ERRVEC
        RST   1         Init. GIC: get evt. inputs
        DATA  :0C       from DCE-bus (bootstrap)
        SHLD  HEAP      Set HEAP start
        RET 
*
        DATA  :02       (not used)
*
********************************************
* part of RUN A VARIABLE REFERENCE (0E95A) *
********************************************
*
MPT49   CALL  RVR05     (0) Run VARPTR
        POP   D
        RET 
*
        DATA  :DD       (not used)
*
*************************************************
* SET INPUT DIRECTION, LOAD SOUND + KEYB TIMERS *
*************************************************
*
* Part of 'stack interrupt' (D9E2).
*
MPT30   STA   EFSW      Set input direction
        CALL  KBIS      Reload keyboard timer
        JMP   SNDIS     Reload sound timer, ret
*
*******************************
* DATA OUTPUT ROUTINE 'DOUTC' *
*******************************
*
* Part of DD70.
* On #02DD, an jump to an user determined output
* routine can be written. As default, a RET is
* on this address.
*
OTC30   POP   PSW       Output char in A
        JMP   DOUTC     Goto user DOUTC
*
********************
* CHECK BREAK FLAG *
********************
*
* Part of 'scan keyboard' (D59A).
*
* Entry: Address 'break' flag in BC.
* Exit:  BCDEHL preserved, AF corrupted.
*
MPT28   LDAX  B         Get KBRFL
        INR   A
        RNZ             Quit if it was <> FF
        STAX  B         KBRFL=0: No recent break
        RET 
*
***************************
* SOUND INTERRUPT (RST 3) *
***************************
*
* Called periodically every few milliseconds.
* Adjust the volume for the sound channels and
* approaches the correct frequency if necessary.
*
* Saves all registers + interrupt mask.
* Enables only clock and sound interrupts.
* Sound interrupt timer is re-loaded and sound
* control blocks are executed.
*
* Entry: HL must already be saved on stack.
* Exit:  All registers preserved.
*        Bank select is restored.
*
SNTMP   PUSH  PSW
        PUSH  B
        PUSH  D
        LDA   CHOPRT    Get current int. mask
        PUSH  PSW       and save it
        MVI   A,:84     )
        STA   CHOPRT    ) Enable clock and sound
        STA   TICC8     ) interrupts only
        EI  
        CALL  SNDIS     Reload sound timer
        LDA   POROM     Get POROM
        PUSH  PSW       and save it
        ANI   :3F
        ORI   :40       Select ROM bank 1
        STA   POROM     Set POROM
        STA   PORO      and PORO
        CALL  TEMPO     (1) Execute SCB('s)
        POP   PSW
        STA   POROM     Re-instate old ROM bank
        STA   PORO      and save it
        JMP   INTRM     Restore int. mask; ret
*
************************
* FAILURE DURING ROPEN *
************************
*
MPT54   DCR   B
        INR   B
        JNZ   RPN20     If file type byte <>0:
 REM Run error
        POP   PSW
        RET 
*
************************************
* CHECK IF LOAD DURING RUN PROGRAM *
************************************
*
* Entry: C: 00 if load during run, else it is FF.
*        A: File type byte.
* Exit:  ABCDEHL preserved.
*
MPT24   DCR   C
        INR   C         Check C
        RZ              Abort if during run
        JMP   FPRNT     Display file type byte!
*
*
*
        UNL
KNSCAN  EQU   :02B9
KBSCAN  EQU   :D59A
LC188   EQU   :CB56
OTSW    EQU   :0131
OTC99   EQU   :DD8C
OUTSE   EQU   :DD94
INKEY   EQU   :E93F
MPT53   EQU   :D808
KBXCT   EQU   :01C1
WBLK    EQU   :02C8
WCLOSE  EQU   :02CB
POROM   EQU   :0040
ROPEN   EQU   :02CE
REXSR   EQU   :E791
CURSOR  EQU   :0072
CURSV   EQU   :0076
OUTC    EQU   :DD60
KLIND   EQU   :02BA
KBRFL   EQU   :02C4
KLIOU   EQU   :02C0
KLIIN   EQU   :02BE
MPT29   EQU   :D1DB
COMP    EQU   :DE14
L1E248  EQU   :EE4C
GTE     EQU   :0090
SMKRM   EQU   :E5A6
TABMA   EQU   :E5A0
SSM21   EQU   :E485
TABM    EQU   :E59A
COL1    EQU   :009D
TABP    EQU   :E539
SS020   EQU   :E43C
STBUSE  EQU   :02A3
RCLOSE  EQU   :02D4
CURRNT  EQU   :0100
WHD20   EQU   :D414
WHDR    EQU   :D40C
SNDINI  EQU   :D8A6
CASIN   EQU   :D795
ERRVEC  EQU   :02EC
HEAP    EQU   :029B
RVR05   EQU   :E96D
EFSW    EQU   :0135
KBIS    EQU   :D99D
SNDIS   EQU   :D9A3
DOUTC   EQU   :02DD
CHOPRT  EQU   :005F
TICC8   EQU   :FFF8
PORO    EQU   :FD06
TEMPO   EQU   :EE6E
INTRM   EQU   :D9CD
RPN20   EQU   :D7DE
FPRNT   EQU   :D7EC
        LST
        END 
